package com.primb.bench.project.home.presenter;

import android.content.Context;

import com.primb.baselibs.util.FormatUtils;
import com.primb.bench.R;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.dialog.SelectListDialog;
import com.primb.bench.common.override.HttpSubscriber;
import com.primb.bench.project.home.contract.AddEnsureInfoContract;
import com.primb.bench.project.home.entity.EnsureObjectEntity;
import com.primb.bench.project.home.entity.HttpApplyResultEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.bench.project.home.entity.KeyEntity;
import com.primb.bench.project.home.model.AddEnsureInfoModelImpl;
import com.primb.netlibs.exception.ApiException;

import java.util.List;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

import static android.R.attr.data;
import static android.R.attr.type;


/**
 * Created by Administrator on 2017/05/17
 */

public class AddEnsureInfoPresenterImpl implements AddEnsureInfoContract.Presenter {
    private AddEnsureInfoModelImpl mModel;
    private AddEnsureInfoContract.View mView;
    private CompositeSubscription subscription;
    public EnsureObjectEntity ensureObjectEntity;
    private String priceId;
    private Context context;
    private String ensureType;

    public AddEnsureInfoPresenterImpl(Context context, AddEnsureInfoContract.View mView, String priceId, EnsureObjectEntity ensureObjectEntity) {
        this.mModel = new AddEnsureInfoModelImpl();
        this.ensureObjectEntity = ensureObjectEntity;
        this.priceId = priceId;
        this.mView = mView;
        this.context = context;
        subscription = new CompositeSubscription();
    }

    @Override
    public void getPickerList(int code, String type) {
        SelectListDialog selectListDialog = mView.showListDialog(code);
        Subscription subscribe = mModel.getPickerList(code, type).subscribe(new HttpSubscriber<List<KeyEntity>>(context) {
            @Override
            public void onError(ApiException e) {
                selectListDialog.dismiss();
                mView.showErrorMsg(e.getMessage());
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onNext(List<KeyEntity> entityList) {
//                if (AppConstant.ADD_MAIN_TYPE == code) {
//                    int position = 0;
//                    boolean isExist = false;
//                    for (int i = 0; i < entityList.size(); i++) {
//                        if (entityList.get(i).getKey().equals("保证")) {
//                            position = i;
//                            isExist = true;
//                            break;
//                        }
//                    }
//                    if (isExist)
//                        entityList.remove(position);
//                }
                selectListDialog.setDateList(entityList);
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public void handleInfoForList(int code, KeyEntity data) {
        if (code == AppConstant.ADD_MAIN_TYPE) {
            ensureObjectEntity.setGuatantDesc(data.getKey());
            ensureObjectEntity.setGuatantCode(data.getValue());
            ensureObjectEntity.setPawnDesc("");
            ensureObjectEntity.setPawnCode("");
        } else {
            ensureObjectEntity.setPawnDesc(data.getKey());
            ensureObjectEntity.setPawnCode(data.getValue());
        }
    }

    @Override
    public boolean isEntityEmpty(EnsureObjectEntity entity) {
        String ensureMoney = mView.getEnsureMoney();
        if (ensureMoney.equals("")) {
            return true;
        }
        entity.setPawnAmt(ensureMoney);
        if (entity.getPawnCode() != null && entity.getPawnCode() != null && !entity.getGuatantCode().equals("")
                && !entity.getGuatantCode().equals("")) {
            entity.setEnsureObjectEmpty(false);
        }
        return entity.isEnsureObjectEmpty();
    }

    @Override
    public void saveEnsureObject(EnsureObjectEntity entity, String priceId, boolean isNew) {
        if (isEntityEmpty(entity)) {
            mView.showErrorMsg("担保信息不完善，请完善后保存");
            return;
        }
        try {
            Double amt = Double.valueOf(entity.getPawnAmt());
            if (amt <= 0) {
                mView.showErrorMsg("担保金额不符合标准");
                return;
            }
        } catch (Exception e) {
            mView.showErrorMsg("担保金额格式有误");
            return;
        }
        mView.showLoading();
        Subscription subscribe = mModel.saveEnsureObject(entity, priceId, isNew).subscribe(new HttpSubscriber<HttpStrEntity>(context) {
            @Override
            public void onCompleted() {
                mView.hideLoading();
            }

            @Override
            public void onNext(HttpStrEntity resultEntity) {
                mView.showErrorMsg(resultEntity.getMsg());
                if (resultEntity.isSuccess())
                    mView.saveResultForBack();
            }

            @Override
            public void onError(ApiException e) {
                mView.hideLoading();
                mView.showErrorMsg(e.getMessage());
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public void onViewClick(int viewId) {
        switch (viewId) {
            case R.id.iv_top:
                mView.resetInfo();
                break;
            case R.id.tv_ensure_object_type:
                if (!mView.getIsNew()) {
                    return;
                }
                if (ensureObjectEntity.getGuatantCode() != null && !ensureObjectEntity.getGuatantCode().equals("")) {
//                    if (ensureObjectEntity.getGuatantDesc().equals("保证"))
//                        getPickerList(0, ensureObjectEntity.getGuatantCode());
//                    else
                    getPickerList(AppConstant.ADD_ENSURE_GOODS, ensureObjectEntity.getGuatantCode());
                } else
                    mView.showErrorMsg("请先选择担保方式");
                break;
            case R.id.tv_ensure_type:
//                if (ensureType.equals("保证"))
//                    return;
                if (!mView.getIsNew()) {
                    return;
                }
                getPickerList(AppConstant.ADD_MAIN_TYPE, "");
                break;
            case R.id.btn_ensure_object_save:
                saveEnsureObject(ensureObjectEntity, priceId, mView.getIsNew());
                break;
        }
    }

    @Override
    public void start() {
//        if (ensureType.equals("保证")) {
//            mView.showLoading();
//            Subscription subscribe = mModel.getPickerList(AppConstant.ADD_MAIN_TYPE, "").subscribe(new HttpSubscriber<List<KeyEntity>>(context) {
//                @Override
//                public void onError(ApiException e) {
//                    mView.hideLoading();
//                    mView.showErrorMsg(e.getMessage());
//                }
//
//                @Override
//                public void onCompleted() {
//                    mView.hideLoading();
//                }
//
//                @Override
//                public void onNext(List<KeyEntity> entityList) {
//                    for (int i = 0; i < entityList.size(); i++) {
//                        if (entityList.get(i).getKey().equals(ensureType)) {
//                            ensureObjectEntity.setGuatantCode(entityList.get(i).getValue());
//                            ensureObjectEntity.setGuatantDesc(entityList.get(i).getKey());
//                            break;
//                        }
//                    }
//                }
//            });
//            subscription.add(subscribe);
//        }
    }

    @Override
    public void onRecycle() {
        subscription.clear();
    }
}